
indexModule.controller('noticeAll',function($scope,$http,$modal,$routeParams){
    //文章id
    $scope.id = $routeParams.id;

    //分页
    $scope.page = {
        page: 1,//当前页数
        limit: 10,// 每页显示多少条
        maxSize: 7 // 显示多少个分页按钮
    };

    if($scope.id){
        //文章内容
        var postData = {id:$scope.id};
        postHttp($http,config.noticeContent_httpUrl,postData,function(data){
            $scope.title = data.data.current.title;
            $scope.content = data.data.current.content;
            if(data.data.pre){
                $scope.preUrl = "#/article/notice/" + data.data.pre._id;
                $scope.preTitle = "【"+data.data.pre.title+"】";
            }else{
                $scope.preUrl = "javascript:;";
                $scope.preTitle = "无";
            }
            if(data.data.next){
                $scope.nextUrl = "#/article/notice/" + data.data.next._id;
                $scope.nextTitle = "【"+data.data.next.title+"】";
            }else{
                $scope.nextUrl = "javascript:;";
                $scope.nextTitle = "无";
            }
        });
    }else{
        //请求所有公告list
        var postData = {page:$scope.page.page,size:$scope.page.limit};
        noticelist(postData,$scope.page.page);
        function noticelist(postData,page){
            postHttp($http,config.noticeAll_httpUrl,postData,function(data){
                $scope.tableData = data.data.list;
                $scope.page.page = page;
                $scope.page.total = data.data.total;// list总数量
                $scope.page.numPages = Math.ceil(data.data.total/$scope.page.limit); // 总页数， 101/20 =6, 这个分页那个自动计算的值。
            });
        };
    }

    //点击搜索
    var hasSearch = false;
    $scope.search = function(){
        var listData = {title:$scope.title,start:$("#search_start").val(),end:$("#search_end").val(),page:1,size:$scope.page.limit};
        noticelist(listData,1);
        hasSearch = true;
    };

    //页码改变时
    $scope.pageChanged = function(){
        if(typeof(localStorage) === "object"){
            localStorage.setItem("limit",$scope.page.limit);
        };
        if(parseInt($scope.page.page) > parseInt($scope.page.numPages)){
            $scope.page.page = parseInt($scope.page.numPages);
        }else if(parseInt($scope.page.page) < 1){
            $scope.page.page = 1;
        }
        if(hasSearch){
            var postData = {title:$scope.title,start:$("#search_start").val(),end:$("#search_end").val(),page:1,size:$scope.page.limit};
        }else{
            var postData = {page:$scope.page.page,size:$scope.page.limit};
        }
        noticelist(postData,$scope.page.page);
    };

});
